import { Component, OnInit } from '@angular/core'
import { FormGroup, FormBuilder } from '@angular/forms'
import { ModalService } from '@app/shared/utils'
import { ModalConfirmComponent } from '@app/shared/components/modal-confirm/modal-confirm.component'
import { NetService, PageService } from '@app/core/http'
import { ApiService } from '../../services/api.service'
import { QlMessageService } from 'qloud-angular'
import { Router } from '@angular/router'
import {CursorService} from "@core/http/cursor.service";
@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.scss'],
  providers: [ModalService, PageService]
})
export class ProductListComponent implements OnInit {
  public catalogList: any[] = []
  public productList: any[] = []
  public id ='';
  constructor(
    public cursorService: CursorService,
    private apiService: ApiService,
    private fb: FormBuilder,
    public modal: ModalService,
    public pageService: PageService,
    private message: QlMessageService,
    private router: Router) {}

  ngOnInit() {
    // this.getProductCatalogList()
    this.cursorService.limit =1000;
    this.apiService.getCatalogList(this.cursorService).subscribe(data => {
      this.catalogList = JSON.parse(JSON.stringify(data).replace(/catalogName/g, 'label'));
      this.catalogList = JSON.parse(JSON.stringify(this.catalogList).replace(/subcatalogs/g, 'children'));
      this.catalogList = JSON.parse(JSON.stringify(this.catalogList).replace(/catalogNo/g, 'id'));
      console.log(JSON.stringify(this.catalogList))

    })

  }

  /**
   * 选择标签类型改变
   * @param param0
   */
  public onCatalogChange($event) {
    console.log(JSON.stringify($event))
    if ($event['action'] === 'click') {
      this.getProductList($event['treeNodeID'])
    }
  }
  onRefresh(){
    this.getProductList(this.id)
  }
  /**
   * 获取标签列表
   * @param parent
   */
  private getProductList(id) {
    this.id = id;
    this.productList =[];
    this.cursorService.limit= 10;
    // this.cursorService.statement="select * where catalogno="+"'"+id+"'",
    this.cursorService.query().eq('catalogno',id)
    this.apiService.getProductList(this.cursorService).subscribe(data => {
      // console.log('list:'+JSON.stringify(data))
      // const product = productList.find(x => x.id === Number(id)) as any
      // const target = productList.find(item => item.id === product.parent)
      // product.productType = target.name
      // return of(product)
      // 生成树形结构
      this.productList = data
    })
    this.cursorService.query({reset:true})
  }

  // private getProductCatalogList() {
  //   this.apiService.getProductList('catalog').subscribe(list => {
  //     // 添加label字段
  //     list.forEach((x: any) => {
  //       x.label = x.name
  //     })
  //     // 生成树形结构
  //     this.catalogList = this.generateCatalogTree(list)
  //   })
  // }

  // private generateCatalogTree(list) {
  //   const root = list.filter(x => !x.parent)
  //   const depth = node => {
  //     const children = list.filter(x => x.parent === node.id)
  //     if (children && children.length) {
  //       node.children = children
  //       node.children.forEach(depth)
  //     } else {
  //       node.leaf = true
  //     }
  //     return node
  //   }
  //
  //   return root.map(depth)
  // }

  public foucus(data) {
    console.log('data'+data.productname)
    let param ={
        catalogName: data.catalogname,
        catalogNo: data.catalogno,
        createDep: data.createdep,
        createUser: data.createuser,
        expireDate: data.expiredate,
        productCode: data.productcode,
        productName: data.productname,
        productNo: data.productno,
        productProfit: data.productprofit,
        recommandType:data.recommandtype,
        recommandTypeName: data.recommandtypename,
        riskLevel: data.risklevel,
        riskLevelName: data.risklevelname,
        productcategory:data.productcategory
    }
    this.modal
      .open({
        title: '确认关注',
        component: ModalConfirmComponent
      })
      .subscribe(() => {
        this.apiService.focusProduct(param).subscribe(data => {
          console.log('----data:' + JSON.stringify(data))
          this.message.success('关注成功')
        })
      })
  }
}
